//
//  PopViewController.m
//  弹出蒙层
//
//  Created by Hello Cai on 2019/6/3.
//  Copyright © 2019 Hello Cai. All rights reserved.
//

#import "PopViewController.h"

@interface PopViewController ()
@property (weak, nonatomic) IBOutlet UIButton *dissmissBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@end

@implementation PopViewController

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    //改变约束
    _bottomConstraint.constant = 0;
    //上述改变约束后，不会立即生效，要手动强制刷新，顺便产生动画
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self.dissmissBtn addTarget:self action:@selector(dismissClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.view.backgroundColor = [UIColor clearColor];
    
    //设置按钮透明度
    self.dissmissBtn.alpha = 0.5f;
    self.dissmissBtn.backgroundColor = [UIColor blackColor];
}

- (IBAction)dismissClick:(id)sender {
//    [self firstDismiss];

    //友盟分享的dismiss方式
    [self secondDismiss];
}

-(void)firstDismiss{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)secondDismiss{
    //改变约束
    _bottomConstraint.constant = -250;
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.dissmissBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];//这句主要是让背景视图和弹窗能同时淡去
        [self.view layoutIfNeeded];    //让约束生效
        
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

@end
